import { defineStore, acceptHMRUpdate } from 'pinia'
import { reactive } from 'vue'
import { getUserInfo } from '@/utils'
const oDefUser: IUser = {
  userId: -1,
  name: '',
  role: '',
  refreshToken: '',
  accessToken: ''
}
export const useUserStore = defineStore('user', () => {
  const oUser = getUserInfo() as IUser
  const oMergeUser: IUser = Object.assign({}, oDefUser)
  if (oUser) {
    Object.assign({}, oUser)
  }
  const userInfo = reactive<IUser>(oMergeUser)

  function setUser(user?: IUser) {
    user = user || oDefUser
    Object.assign(userInfo, user)
  }
  return { userInfo, setUser }
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useUserStore, import.meta.hot))
}
